//
//  STMTopTenFlowViewController.m
//  StoreManagement
//
//  Created by centling on 16/5/17.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMTopTenFlowViewController.h"
#import "STMTopTenFlowDetailCell.h"
#import "STMLoginViewController.h"

@interface STMTopTenFlowViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *orderStatisticsArrayA;
@property (nonatomic, strong) NSMutableArray *orderStatisticsArrayB;

@end

@implementation STMTopTenFlowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    if ([self.VCindex isEqualToString:@"4"] || [self.VCindex isEqualToString:@"5"] || [self.VCindex isEqualToString:@"7"] || [self.VCindex isEqualToString:@"9"]) {
        self.tableView.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H-108);
    }
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"STMTopTenFlowDetailCell" bundle:nil] forCellReuseIdentifier:@"cellID"];
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    [self loadViewData];
}


- (void)loadViewData {
    [SVProgressHUD showWithStatus:@"加载中..."];
    if ([self.VCindex isEqualToString:@"1"]) {
        [STMNetworkAPI getTopTenFLowWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                    success:^(STMTopGoodsflowModel *result) {
                                        if ([result.code isEqualToString:@"200"]) {
                                            [SVProgressHUD dismiss];
                                            self.dataArray = [NSMutableArray arrayWithArray:result.result.b];
                                            [self.tableView reloadData];
                                            
                                        } else if ([result.code isEqualToString:@"401"]) {
                                            [GlobalUtil presentLoginVC];
                                        } else {
                                            [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                        }
                                    }
                                    failure:^(NSError *err) {
                                        [SVProgressHUD dismiss];
                                        [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                    }];

    }
    if ([self.VCindex isEqualToString:@"2"]) {
        [STMNetworkAPI getTopTenMoneyAmountWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                           success:^(STMTopGoodsflowModel *result) {
                                            if ([result.code isEqualToString:@"200"]) {
                                                [SVProgressHUD dismiss];
                                                self.dataArray = [NSMutableArray arrayWithArray:result.result.b];
                                                [self.tableView reloadData];
                                                
                                            } else if ([result.code isEqualToString:@"401"]) {
                                                [GlobalUtil presentLoginVC];
                                            } else {
                                                [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                            }
                                        }
                                        failure:^(NSError *err) {
                                            [SVProgressHUD dismiss];
                                            [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                        }];
        
    }
    if ([self.VCindex isEqualToString:@"3"]) {
        [STMNetworkAPI getTopTenOrderAmountWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                           success:^(STMTopGoodsflowModel *result) {
                                               if ([result.code isEqualToString:@"200"]) {
                                                   [SVProgressHUD dismiss];
                                                   self.dataArray = [NSMutableArray arrayWithArray:result.result.b];
                                                   [self.tableView reloadData];
                                                   
                                               } else if ([result.code isEqualToString:@"401"]) {
                                                   [GlobalUtil presentLoginVC];
                                               } else {
                                                   [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                               }
                                           }
                                           failure:^(NSError *err) {
                                               [SVProgressHUD dismiss];
                                               [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                           }];
        
    }
    if ([self.VCindex isEqualToString:@"4"]) {
        [STMNetworkAPI getOrderAmountWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                     statype:@"week"
                                     success:^(STMOrderStatisticsModel *result) {
                                         if ([result.code isEqualToString:@"200"]) {
                                             [SVProgressHUD dismiss];
                                             self.dataArray = [NSMutableArray arrayWithArray:result.result.b];
                                             self.orderStatisticsArrayA = [NSMutableArray arrayWithArray:result.result.a];
                                             self.orderStatisticsArrayB = [NSMutableArray arrayWithArray:result.result.b];
                                             [self.tableView reloadData];
                                             
                                         } else if ([result.code isEqualToString:@"401"]) {
                                             [GlobalUtil presentLoginVC];
                                         } else {
                                             [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                         }
                                     }
                                     failure:^(NSError *err) {
                                         [SVProgressHUD dismiss];
                                         [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                     }];
        
    }
    if ([self.VCindex isEqualToString:@"5"]) {
        [STMNetworkAPI getOrderAmountWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                     statype:@"month"
                                     success:^(STMOrderStatisticsModel *result) {
                                         if ([result.code isEqualToString:@"200"]) {
                                             [SVProgressHUD dismiss];
                                             self.dataArray = [NSMutableArray arrayWithArray:result.result.b];
                                             self.orderStatisticsArrayA = [NSMutableArray arrayWithArray:result.result.a];
                                             self.orderStatisticsArrayB = [NSMutableArray arrayWithArray:result.result.b];
                                             [self.tableView reloadData];
                                             
                                         } else if ([result.code isEqualToString:@"401"]) {
                                             [GlobalUtil presentLoginVC];
                                         } else {
                                             [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                         }
                                     }
                                     failure:^(NSError *err) {
                                         [SVProgressHUD dismiss];
                                         [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                     }];
        
    }
    if ([self.VCindex isEqualToString:@"6"]) {
        [STMNetworkAPI getOrderNumWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                     statype:@"week"
                                     success:^(STMOrderStatisticsModel *result) {
                                         if ([result.code isEqualToString:@"200"]) {
                                             [SVProgressHUD dismiss];
                                             self.dataArray = [NSMutableArray arrayWithArray:result.result.b];
                                             self.orderStatisticsArrayA = [NSMutableArray arrayWithArray:result.result.a];
                                             self.orderStatisticsArrayB = [NSMutableArray arrayWithArray:result.result.b];
                                             [self.tableView reloadData];
                                             
                                         } else if ([result.code isEqualToString:@"401"]) {
                                             [GlobalUtil presentLoginVC];
                                         } else {
                                             [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                         }
                                     }
                                     failure:^(NSError *err) {
                                         [SVProgressHUD dismiss];
                                         [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                     }];
    }
    if ([self.VCindex isEqualToString:@"7"]) {
        [STMNetworkAPI getOrderNumWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                  statype:@"month"
                                  success:^(STMOrderStatisticsModel *result) {
                                      if ([result.code isEqualToString:@"200"]) {
                                          [SVProgressHUD dismiss];
                                          self.dataArray = [NSMutableArray arrayWithArray:result.result.b];
                                          self.orderStatisticsArrayA = [NSMutableArray arrayWithArray:result.result.a];
                                          self.orderStatisticsArrayB = [NSMutableArray arrayWithArray:result.result.b];
                                          [self.tableView reloadData];
                                          
                                      } else if ([result.code isEqualToString:@"401"]) {
                                          [GlobalUtil presentLoginVC];
                                      } else {
                                          [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                      }
                                  }
                                  failure:^(NSError *err) {
                                      [SVProgressHUD dismiss];
                                      [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                  }];
    }
    if ([self.VCindex isEqualToString:@"8"] || [self.VCindex isEqualToString:@"9"]) {
        [STMNetworkAPI getStoreFlowWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                  stattype:[self.VCindex isEqualToString:@"8"] ? @"week" : @"month"
                                   success:^(STMStoreFlowModel *result) {
                                       if ([result.code isEqualToString:@"200"]) {
                                           [SVProgressHUD dismiss];
                                           self.dataArray = [NSMutableArray arrayWithArray:result.result.xAxis.categories];
                                           self.orderStatisticsArrayA = [NSMutableArray arrayWithArray:result.result.xAxis.categories];
                                           STMStoreFlowSeries *series = result.result.series[0];
                                           self.orderStatisticsArrayB = [NSMutableArray arrayWithArray:series.data];
                                           [self.tableView reloadData];
                                           
                                       } else if ([result.code isEqualToString:@"401"]) {
                                           [GlobalUtil presentLoginVC];
                                       } else {
                                           [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                       }
                                   }
                                   failure:^(NSError *err) {
                                       [SVProgressHUD dismiss];
                                       [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                   }];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    STMTopTenFlowDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    if ([self.VCindex isEqualToString:@"4"] || [self.VCindex isEqualToString:@"5"] || [self.VCindex isEqualToString:@"6"] || [self.VCindex isEqualToString:@"7"] || [self.VCindex isEqualToString:@"8"] || [self.VCindex isEqualToString:@"9"]) {
        cell.goodsnameLabel.text = [NSString stringWithFormat:@"%@",self.orderStatisticsArrayA[indexPath.row]];
        cell.flowNumLabel.text = [NSString stringWithFormat:@"%@",self.orderStatisticsArrayB[indexPath.row]];
    } else {
        STMTopGoodsflowResultB *cellData = self.dataArray[indexPath.row];
        cell.goodsnameLabel.text = cellData.name;
        cell.flowNumLabel.text = cellData.y;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
